package com.test.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.controller.Admininfo.ResData;
import com.test.entity.CwInfo;
import com.test.entity.JobInfo;
import com.test.service.DepartInfoService;
import com.test.service.JobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 职位(JobInfo)表控制层
 *
 * @author makejava
 * @since 2022-11-12 11:18:51
 */
@Controller
public class AdminJobInfoController {


    @Autowired
    private JobInfoService jobInfoService;
    @Autowired
    private DepartInfoService departInfoService;

    //    显示职位页面信息
    @GetMapping("admin/job_info")
    public String getCwInfo() {
        return "admin/job_info/list";
    }

    //    显示职位管理信息
    @GetMapping("admin/job_info/queryList")
    @ResponseBody
    public String getQueryList(Integer page, JobInfo jobInfo) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(jobInfoService.queryByPage(jobInfo, PageRequest.of(page - 1, 10)));
    }

    //    跳转职位新增页面
    @GetMapping("admin/job_info/add")
    public ModelAndView getAdd() {
        ModelAndView modelAndView = new ModelAndView("admin/job_info/add_page");
        modelAndView.addObject("departInfoList",departInfoService.selectAll());
        return modelAndView;
    }

    //    提交职位新增
    @PostMapping("admin/job_info/add_submit")
    @ResponseBody
    public ResData postAddSubmit(JobInfo jobInfo) {
        ResData resData = new ResData();
        int insert = jobInfoService.insert(jobInfo);
        if (insert>0) {
            resData.setCode(200);
            resData.setMsg("添加成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("添加失败");
        return resData;
    }

    //    跳转职位修改页面
    @GetMapping("admin/job_info/update")
    public ModelAndView getUpdate(Integer id) {
        ModelAndView modelAndView = new ModelAndView("admin/job_info/update_page");
        JobInfo jobInfo = jobInfoService.queryById(id);
        modelAndView.addObject("data", jobInfo);
        modelAndView.addObject("departInfoList",departInfoService.selectAll());
        return modelAndView;
    }

    //    提交职位修改页面信息
    @PostMapping("admin/job_info/update_submit")
    @ResponseBody
    public ResData postUpdateSubmit(Integer id,JobInfo jobInfo) {
        jobInfo.setId(id);
        ResData resData = new ResData();
        JobInfo update = jobInfoService.update(jobInfo);
        if (update.getId() != null || !update.getId().equals("")) {
            resData.setCode(200);
            resData.setMsg("修改成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("修改失败");
        return resData;
    }

    //    删除财务页面信息
    @GetMapping("admin/job_info/del")
    @ResponseBody
    public ResData getDelete(Integer id) {
        ResData resData = new ResData();
        boolean b = jobInfoService.deleteById(id);
        if (b) {
            resData.setCode(1);
            resData.setMsg("删除成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("删除失败");
        return resData;
    }

}

